package model

import (
	"gorm.io/gorm/clause"
	"server/common"
	"server/pkg/app"
)

type BaseCase struct {
	Model
	CaseID int    `gorm:"column:caseid;" json:"caseid"`
	Name   string `gorm:"column:name;not null;" json:"name"`

	ProjectID int     `json:"project_id"`
	Project   Project `gorm:"references:ID"`

	Labels []TestCaseLabel `gorm:"foreignkey:ID" json:"labels"` //自引用has Many

	CaseMeauID int      `gorm:"column:father" json:"father"`
	CaseMeau   CaseMeau `gorm:"references:ID"`
}

type TestCase struct {
	BaseCase
}

func (c *TestCase) TableName() string {
	return "testcase"
}
func (c *BaseCase) TableName() string {
	return "testcase"
}
func (c *TestCase) List(page *app.Page) ([]TestCase, int64) {
	testcases := []TestCase{}
	var count int64
	if err := common.DB.Debug().Order("id desc").Limit(page.Size).Offset((page.Page - 1) * page.Size).Find(&testcases).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
		return testcases, count
	}
	return testcases, count
}

func (c *TestCase) Info() TestCase {
	var testcase TestCase
	if err := common.DB.Where("name = ?", c.Name).Find(&testcase).Error; err != nil {
		return testcase
	}
	return testcase
}

func (c *TestCase) InfoById() {
	if err := common.DB.Where("id = ?", c.ID).Find(&c).Error; err != nil {
		return
	}
}

func (c *TestCase) SearchByName(page *app.Page, key string) ([]TestCase, int64, error) {
	pipes := []TestCase{}
	var count int64
	if err := common.DB.Where("name like ?", "%"+key+"%").Order("starttime desc").Limit(page.Size).Offset((page.Page - 1) * page.Size).Find(&pipes).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
		return pipes, count, err
	}
	return pipes, count, nil
}

func (c *TestCase) Create() error {
	if err := common.DB.Model(&TestCase{}).Omit(clause.Associations).Create(&c).Error; err != nil {
		return err
	}
	return nil
}

func (c *TestCase) Update() error {
	if err := common.DB.Where("name = ?", c.Name).Updates(&c).Error; err != nil {
		return err
	}
	return nil
}

func (c *TestCase) Delete() error {
	if err := common.DB.Where("name = ?", c.Name).Delete(&c).Error; err != nil {
		return err
	}
	return nil
}

func (c *TestCase) QueryById(id string, page *app.Page) ([]TestCase, int64) {
	case_list := []TestCase{}
	var count int64
	if err := common.DB.Model(&TestCase{}).Debug().Where("father_md5 = ?", id).Limit(page.Size).Offset((page.Page - 1) * page.Size).Find(&case_list).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
		return case_list, count
	}
	return case_list, count
}

func QueryByIds(id []int64, page *app.Page) ([]TestCase, int64) {
	case_list := []TestCase{}
	var count int64
	if err := common.DB.Model(&TestCase{}).Where("id in ?", id).Limit(page.Size).Offset((page.Page - 1) * page.Size).Find(&case_list).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
		return case_list, count
	}
	return case_list, count
}

func QueryAllById(id string) []TestCase {
	case_list := []TestCase{}
	if err := common.DB.Model(&TestCase{}).Where("father = ?", id).Find(&case_list).Error; err != nil {
		return case_list
	}
	return case_list
}

func QueryAllByLabel(ids []int) []TestCase {
	case_list := []TestCase{}
	if err := common.DB.Model(&TestCase{}).Where("label in ?", ids).Find(&case_list).Error; err != nil {
		return case_list
	}
	return case_list
}

func QueryAllInById(id []int64) []TestCase {
	case_list := []TestCase{}
	if err := common.DB.Model(&TestCase{}).Where("father IN ?", id).Find(&case_list).Error; err != nil {
		return case_list
	}
	return case_list
}

func ListFatherNull() []TestCase {
	var testcases []TestCase
	if err := common.DB.Debug().Where("father is NULL").Find(&testcases).Error; err != nil {
		return testcases
	}
	return testcases
}
